//
//  UIViewController+Saas.m
//  saas
//
//  Created by fangyibao on 2019/10/10.
//  Copyright © 2019 fangmao. All rights reserved.
//

#import "UIViewController+Saas.h"
#import <objc/runtime.h>
@implementation UIViewController (Saas)
+ (UIViewController *)findBestViewController:(UIViewController*)vc{
    if (vc.presentedViewController) {
            // Return presented view controller
        return [self findBestViewController:vc.presentedViewController];
    } else if ([vc isKindOfClass:[UISplitViewController class]]) {
            // Return right hand side
        UISplitViewController* svc = (UISplitViewController*) vc;
        if (svc.viewControllers.count > 0)
            return [self findBestViewController:svc.viewControllers.lastObject];
        else
            return vc;
    } else if ([vc isKindOfClass:[UINavigationController class]]) {
            // Return top view
        UINavigationController* svc = (UINavigationController*) vc;
        if (svc.viewControllers.count > 0)
            return [self findBestViewController:svc.visibleViewController];
        else
            return vc;
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
            // Return visible view
        UITabBarController* svc = (UITabBarController*) vc;
        if (svc.viewControllers.count > 0)
            return [self findBestViewController:svc.selectedViewController];
        else
            return vc;
    } else {
            // Unknown view controller type, return last child view controller
        return vc;
    }
}
- (void)showAlert:(NSDictionary *)alert cancel:(void (^)())cancel confirm:(void (^)())confirm{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alert[@"title"] message:alert[@"message"] preferredStyle:UIAlertControllerStyleAlert];
    if (alert[@"confirm"]) {
        UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:alert[@"confirm"] style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnull action) {
            if (confirm) confirm();
        }];
        [alertController addAction:confirmAction];
    }
    
    if (alert[@"cancel"]) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:alert[@"cancel"] style:UIAlertActionStyleCancel handler:^(UIAlertAction*_Nonnull action) {
            if (cancel) cancel();
        }];

        [alertController addAction:cancelAction];
    }
    
    [self presentViewController:alertController animated:YES completion:nil];
}
- (void)showActionSheet:(NSArray<NSDictionary<NSString *,void (^)()> *> *)sheetArray{
    UIAlertController *avc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    for (NSDictionary *sheet in sheetArray) {
        UIAlertAction *a = [UIAlertAction actionWithTitle:sheet.allKeys.firstObject style:UIAlertActionStyleDefault handler:sheet[sheet.allKeys.firstObject]];
        [avc addAction:a];
    }
    UIAlertAction *ca = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [avc addAction:ca];
    [self presentViewController:avc animated:YES completion:nil];
}
@end
